package org.jim.server.demo.messageConverter;

import org.jim.core.ImChannelContext;
import org.jim.core.packets.ChatBody;
import org.jim.core.packets.Command;
import org.jim.core.utils.JsonKit;
import org.jim.server.adapter.BaseMessageConverter;

/**
 * 聊天消息转换器，处理ChatBody格式
 */
public class ChatBodyMessageConverter extends BaseMessageConverter {
    @Override
    public boolean supports(byte[] body) {
        try {
            // 尝试解析为ChatBody判断是否支持
            ChatBody chatBody = JsonKit.toBean(body, ChatBody.class);
            return Command.COMMAND_CHAT_REQ.getNumber() == chatBody.getCmd();
        } catch (Exception e) {
            return false;
        }
    }

    @Override
    public ChatBody convert(byte[] body, ImChannelContext channelContext) {
        // 将ChatBody转换为Message（ChatBody继承自Message）
        ChatBody message = JsonKit.toBean(body, ChatBody.class);
        return message;
    }
}
